1 # importing Tkinter and math
2 from tkinter import *
3 import math
4
5 # calc class
6 class calc:
7
8 def getandreplace(self):
9
10 """replace x with * and ÷ with /"""
11 self.expression = self.e.get()
12 self.newtext=self.expression.replace('/','/')
13 self.newtext=self.newtext.replace('x','*')
14
15
16 def equals(self):
17 """when the equal button is pressed"""
18 self.getandreplace()
19 try:
20 # evaluate the expression using the eval function
21 self.value= eval(self.newtext)
22 except SyntaxError or NameError:
23 self.e.delete(0,END)
24 self.e.insert(0,'Invalid Input!')
25 else:
26 self.e.delete(0,END)
27 self.e.insert(0,self.value)
28
29 def squareroot(self):
30 """squareroot method"""
31 self.getandreplace()
32 try:
33 # evaluate the expression using the eval function
34 self.value= eval(self.newtext)
35 except SyntaxError or NameError:
36 self.e.delete(0,END)
37 self.e.insert(0,'Invalid Input!')
38 else:
39 self.sqrtval=math.sqrt(self.value)
40 self.e.delete(0,END)
41 self.e.insert(0,self.sqrtval)
42
43 def square(self):
44 """square method"""
45 self.getandreplace()
46 try:
47 #evaluate the expression using the eval function
48 self.value= eval(self.newtext)
49 except SyntaxError or NameError:
50 self.e.delete(0,END)
51 self.e.insert(0,'Invalid Input!')
52 else:
53 self.sqval=math.pow(self.value,2)
54 self.e.delete(0,END)
55 self.e.insert(0,self.sqval)
56
57 def clearall(self):
58 """when clear button is pressed,clears the text input area"""
59 self.e.delete(0,END)
60
61 def clear1(self):
62 self.txt=self.e.get()[:-1]
63 self.e.delete(0,END)
64 self.e.insert(0,self.txt)
65
66 def action(self,argi):
67 """pressed button's value is inserted into the end of the text area"""
68 self.e.insert(END,argi)
69
70 def __init__(self,master):
71 """Constructor method"""
72 master.title('Calulator')
73 master.geometry()
74 self.e = Entry(master)
75 self.e.grid(row=0,column=0,columnspan=6,pady=3)
76 self.e.focus_set() #Sets focus on the input text area
77
78 # Generating Buttons
79 Button(master,text="=",width=11,height=3,fg="blue",
80 bg="orange",command=lambda:self.equals()).grid(
81 row=4, column=4,columnspan=2)
82
83 Button(master,text='AC',width=5,height=3,
84 fg="red", bg="light green",
85 command=lambda:self.clearall()).grid(row=1, column=4)
86
87 Button(master,text='C',width=5,height=3,
88 fg="red",bg="light green",
89 command=lambda:self.clear1()).grid(row=1, column=5)
90
91 Button(master,text="+",width=5,height=3,
92 fg="blue",bg="orange",
93 command=lambda:self.action('+')).grid(row=4, column=3)
94
95 Button(master,text="x",width=5,height=3,
96 fg="blue",bg="orange",
97 command=lambda:self.action('x')).grid(row=2, column=3)
98
99 Button(master,text="-",width=5,height=3,
100 fg="red",bg="light green",
101 command=lambda:self.action('-')).grid(row=3, column=3)
102
103 Button(master,text="÷",width=5,height=3,
104 fg="blue",bg="orange",
105 command=lambda:self.action('/')).grid(row=1, column=3)
106
107 Button(master,text="%",width=5,height=3,
108 fg="red",bg="light green",
109 command=lambda:self.action('%')).grid(row=4, column=2)
110
111 Button(master,text="7",width=5,height=3,
112 fg="blue",bg="orange",
113 command=lambda:self.action('7')).grid(row=1, column=0)
114
115 Button(master,text="8",width=5,height=3,
116 fg="red",bg="light green",
117 command=lambda:self.action(8)).grid(row=1, column=1)
118
119 Button(master,text="9",width=5,height=3,
120 fg="blue",bg="orange",
121 command=lambda:self.action(9)).grid(row=1, column=2)
122
123 Button(master,text="4",width=5,height=3,
124 fg="red",bg="light green",
125 command=lambda:self.action(4)).grid(row=2, column=0)
126
127 Button(master,text="5",width=5,height=3,
128 fg="blue",bg="orange",
129 command=lambda:self.action(5)).grid(row=2, column=1)
130
131 Button(master,text="6",width=5,height=3,
132 fg="white",bg="blue",
133 command=lambda:self.action(6)).grid(row=2, column=2)
134
135 Button(master,text="1",width=5,height=3,
136 fg="red",bg="light green",
137 command=lambda:self.action(1)).grid(row=3, column=0)
138
139 Button(master,text="2",width=5,height=3,
140 fg="blue",bg="orange",
141 command=lambda:self.action(2)).grid(row=3, column=1)
142
143 Button(master,text="3",width=5,height=3,
144 fg="white",bg="blue",
145 command=lambda:self.action(3)).grid(row=3, column=2)
146
147 Button(master,text="0",width=5,height=3,
148 fg="white",bg="blue",
149 command=lambda:self.action(0)).grid(row=4, column=0)
150
151 Button(master,text=".",width=5,height=3,
152 fg="red",bg="light green",
153 command=lambda:self.action('.')).grid(row=4, column=1)
154
155 Button(master,text="(",width=5,height=3,
156 fg="white",bg="blue",
157 command=lambda:self.action('(')).grid(row=2, column=4)
158
159 Button(master,text=")",width=5,height=3,
160 fg="blue",bg="orange",
161 command=lambda:self.action(')')).grid(row=2, column=5)
162
163 Button(master,text="?",width=5,height=3,
164 fg="red",bg="light green",
165 command=lambda:self.squareroot()).grid(row=3, column=4)
166
167 Button(master,text="x²",width=5,height=3,
168 fg="white",bg="blue",
169 command=lambda:self.square()).grid(row=3, column=5)
170
171 # Driver Code
172 root = Tk()
173
174 obj=calc(root) # object instantiated
175
176 root.mainloop()